<?php
namespace app\common\widget\send;
use app\common\helper\appmsg_helper;
use app\common\helper\appmsg_send_helper;
use app\common\helper\user_helper;
use app\common\model\SendQueue;
use app\common\model\SendTask;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/8
 * Time: 18:00
 */
class send_task_widget
{
    private $per_page = 10;

    /**
     * 根据发送任务，合并新图文、组装发送队列
     */
    public function do_send_task()
    {
        //echo '11';exit;
        $need_filter = array(
            ['progress', '>=' , 1 ],
            ['progress', '<='  , 2 ]
        );

        $need_send_task = _uri('send_task', $need_filter, '', ['id', 'asc']);
        if (!$need_send_task) {
            echo 'no more';
            return;
        }
        if ($need_send_task['progress'] == 2) {
            //echo 'send_task:['.$need_send_task['id'].'] 正在处理中';
            //return;
        }

        SendTask::update(['progress'=>2], ['id'=>$need_send_task['id']]);

        $send_target = json_decode($need_send_task['send_target'], true);
        if (!$send_target || !isset($send_target['store_id'])) {
            $fail_reason =  '请选择发送的目标';
            SendTask::update(['progress'=>15, 'reason'=>$fail_reason], ['id'=>$need_send_task['id']]);
            return $fail_reason;
        }
        //var_dump($send_target);exit;

        // 选中的要发的总后台的图文
        $main_appmsg_list = appmsg_helper::get_appmsg_list($need_send_task['content']);

        $store_ids = $send_target['store_id'];

        // 去除重复
        $store_ids = array_unique($store_ids);
        // 升序
        sort($store_ids);

        foreach ($store_ids as $k=>$v) {

            $pre_store_id = $v;

            // 检查是否已添加这个队列，如果已添加，则不再添加
            $old_send_filter = array(
                'send_task_id'  => $need_send_task['id'],
                'to_store_id'   => $pre_store_id
            );

            $old_send_queue_info = _uri('send_queue', $old_send_filter);
            if ($old_send_queue_info) {
                continue;
            }

            //创建各店铺拼接， 队列
            // 如果有店铺id，需要获取每个店铺下的图文，和总店铺的图文合并到一起，生成新图文
            $store_appmsg_list = array();
            if ($pre_store_id) {
                // 取店铺的appmsg信息
                $store_appmsg_filter = array(
                    'merge_id'  => 0,
                    'store_id'  => $v,
                    'type'      => 1,   // 只取店铺的单图文
                    'verify'    => 1,
                    'send'      => 2,
                    'status'    => 1
                );
                $store_appmsg_list = appmsg_helper::get_store_appmsg_list($store_appmsg_filter);
                $store_appmsg_list = $store_appmsg_list['data'];
            }

            //dump($pre_store_id, $main_appmsg_list, $store_appmsg_list);
            // 在appmsg_send中，生成新的合并后的图文
            $appmsg_send_id = appmsg_send_helper::add_multi_appmsg_send($main_appmsg_list, $store_appmsg_list, $pre_store_id, $need_send_task['id']);
            //continue;

            // 根据它是否为空，来决定appmsg_send是否有要更新的信息
            $update_appmsg_send = array();

            // 生成send_queue记录
            if ($appmsg_send_id) {
                //$user_num = get_res_count('user', array('store_id'=>$pre_store_id, 'is_subscribe'=>1));
                $user_num = user_helper::get_user_num_by_filter(array('store_id'=>$pre_store_id, 'is_subscribe'=>1));
                $progress = 0;
                $reason = '';
                if ($user_num < 1) {
                    $progress = 15;
                    $reason = '该店铺下没有成员';

                    $update_appmsg_send['media_id'] = 'store has no user';
                }

                $new_queue_info = array(
                    'user_id'       => $need_send_task['user_id'],
                    'send_task_id'  => $need_send_task['id'],
                    'to_store_id'   => $pre_store_id,
                    'type'          => $need_send_task['type'],
                    'content'       => $appmsg_send_id, // 要发送的图文id，即新的appmsg_send的id
                    'progress'      => $progress,
                    'reason'        => $reason
                );

                $new_send_queue    = SendQueue::create($new_queue_info);
                $new_send_queue_id = $new_send_queue->id;
                if ($new_send_queue_id) {
                    $update_appmsg_send['send_queue_id'] = $new_send_queue_id;
                }
            }

            if ($update_appmsg_send) {
                update_res_field('appmsg_send', $appmsg_send_id, $update_appmsg_send);
            }
        }

        // 更改任务的进行状态
        SendTask::update(['progress'=>10], ['id'=>$need_send_task['id']]);

        echo 'success';
    }
}